/** @jsxImportSource @emotion/react */
import {
  BlobInputList, CCell, Cell, Input, InputLine, LineColumn, SuffixInput, Table, TableBody, TableHead, TableRow, Text
} from "customize-easy-ui-component";
import * as React from "react";
import {InspectRecordLayout, InternalItemProps, RepLink, SelectInput, useItemInputControl} from "../../common/base";
import {useMeasureInpFilter} from "../../common/hooks";
import {findMaxByField, 检查结论选} from "../../tools";
import {css} from "@emotion/react";
import {天气选, 路面选} from "./Speed";
import {Each_ZdSetting,useRaftTableViewerX, useTableEditor} from "../../hook/useRepTableEditor";
import {expandDimension} from "../../../common/tool";

export const tail温度试= <Text css={{"@media print": {fontSize: '0.75rem'}}}>
  注：1、将测得的最高温度修正到40℃环境温度时的温度值。<br/>
  2、在规定的试验条件下，每完成5个试验循环之后进行温度测量，在测量温度后，车辆直接继续进行下一个试验循环。试验一旦开始不得停止，一直进行到温升稳定在2K/h及以下。<br/>
  3、在T1、T2和T3组车辆上配置的液压系统，不必测量其表面温度。<br/>
  4、对于无需测量的，测量值栏可不填。
</Text>;
const config温度试 =[['制动器','b',60], ['离合器','l',60,], ['液压系统','y',80,], ['行走电机','x',80,], ['发动机','f',60] ] as Each_ZdSetting[];
export const itemA温度试= ['天气爆温','温度爆温','风速爆温','湿度爆温','路面爆温', '爆温度表','爆温度x','爆温度s','爆温度v','爆温度r','最表面温'] as string[];
export const ExplosionProof=
    React.forwardRef((
        { children, show ,alone=true,redId,nestMd,label,}:InternalItemProps,  ref
    ) => {
      const [getInpFilter]=useMeasureInpFilter(null,itemA温度试,);
      const {inp, setInp} = useItemInputControl({ ref });
      const headview=<Text variant="h6">
        多次测量底下部位的温度，单位（℃）
      </Text>;
      const [render]=useTableEditor({inp, setInp,  headview,
              config: config温度试, table: '爆温度表',  maxRf:3 });
      const mxTemp = React.useMemo(() =>config温度试.map(([_, tag,]:any, c:number) => {
          let val=findMaxByField(inp?.爆温度表??[], tag);
          return -Infinity===val? '未知' : val
      }), [inp?.爆温度表]);
      return <InspectRecordLayout inp={inp} setInp={setInp} getInpFilter={getInpFilter} show={show} redId={redId}
                                  nestMd={nestMd} alone={alone} label={label!}>
        <Text variant="h5">{label}：</Text>
        试验条件：
        <div css={{display: 'flex', flexWrap: 'wrap', alignItems: 'center', justifyContent: 'center'}}>
          <div css={{display: 'inline-flex', alignItems: 'center', flexWrap: 'wrap'}}>
            天气:
            <BlobInputList value={inp?.天气爆温 || ''} rows={1} datalist={天气选}
                           style={{display: 'inline-flex', width: '5rem', maxWidth: '100%', fontSize: '1rem',}}
                           listStyle={css({fontSize: '1rem',})}
                           onListChange={v => setInp({...inp, 天气爆温: v || undefined})}/>
          </div>
          <div css={{display: 'inline-flex', alignItems: 'center', flexWrap: 'wrap'}}>
            温度：
            <Input value={inp?.温度爆温 || ''} size={2} style={{display: 'inline-flex', width: 'unset'}}
                   onChange={e => setInp({...inp, 温度爆温: e.currentTarget.value || undefined})}/>
            ℃；
          </div>
          <div css={{display: 'inline-flex', alignItems: 'center', flexWrap: 'wrap'}}>
            最大风速：
            <Input value={inp?.风速爆温 || ''} size={2} style={{display: 'inline-flex', width: 'unset'}}
                   onChange={e => setInp({...inp, 风速爆温: e.currentTarget.value || undefined})}/>
            m/s；
          </div>
          <div css={{display: 'inline-flex', alignItems: 'center', flexWrap: 'wrap'}}>
            空气最大相对湿度:
            <Input value={inp?.湿度爆温 || ''} size={2} style={{display: 'inline-flex', width: 'unset'}}
                   onChange={e => setInp({...inp, 湿度爆温: e.currentTarget.value || undefined})}/>
            %；
          </div>
          <div css={{display: 'inline-flex', alignItems: 'center', flexWrap: 'wrap'}}>
            试验路面类型：
            <BlobInputList value={inp?.路面爆温 || ''} rows={1} datalist={路面选}
                           style={{display: 'inline-flex', width: '19rem', maxWidth: '100%', fontSize: '1rem',}}
                           listStyle={css({fontSize: '1rem',})}
                           onListChange={v => setInp({...inp, 路面爆温: v || undefined})}/>
          </div>
        </div>
          制动器、离合器、液压系统、行走电机及发动机按照GB/T 19854—2018中5.2规定的试验方法进行温度试验，测得的最高表面温度不应当超过车辆的设计最高表面温度，即（
          <Input value={inp?.最表面温 || ''} size={4} style={{display: 'inline-flex', width: 'unset'}}
                onChange={e => setInp({...inp, 最表面温: e.currentTarget.value || undefined})}/>
          ）℃。
        <hr/>
        {render}
        <hr/>
        {config温度试.map(([title, _,]:any, c:number) => {
          return  `${title} 最高温= ${mxTemp[c]} ；`
        })}
        <LineColumn>
          <InputLine label='温度修正值'>
            <SuffixInput value={inp?.爆温度x || ''}
                         onChange={e => setInp({...inp, 爆温度x: e.currentTarget.value || undefined})}>℃</SuffixInput>
          </InputLine>
          <InputLine label='温度要求值(℃)'>
            <BlobInputList value={inp?.爆温度s || ''} rows={1} datalist={[inp?.最表面温??'', ]}
                           onListChange={v => setInp({...inp, 爆温度s: v || undefined})}/>
          </InputLine>
          <InputLine label='温度测量-结果'>
            <SuffixInput value={inp?.爆温度v || ''}
                         onChange={e => setInp({...inp, 爆温度v: e.currentTarget.value || undefined})}>℃</SuffixInput>
          </InputLine>
        </LineColumn>
        <LineColumn>
          <InputLine label='C3.11.1结果判定'>
            <SelectInput value={inp?.爆温度r || ''} list={检查结论选}
                         onChange={e => setInp({...inp, 爆温度r: e.currentTarget.value || undefined})}/>
          </InputLine>
        </LineColumn>
        {tail温度试}
      </InspectRecordLayout>;
});
//附表13 防爆功能温度试
export const ExplosionProofVw = ({orc, rep, label,}: { orc: any, rep: any, label: string }
) => {
  const rowspa=orc?.爆温度表?.length || 1;
  const mergeR=<>
          <CCell split={rowspa > 7} rowSpan={rowspa}>{orc?.爆温度x}</CCell><CCell split rowSpan={rowspa}>{orc?.爆温度s}</CCell>
          <CCell split rowSpan={rowspa}>{orc?.爆温度v}</CCell><CCell split rowSpan={rowspa}>{orc?.爆温度r}</CCell>
      </>;
  const [renderRows ]=useRaftTableViewerX({config:config温度试,content:orc?.爆温度表,
               seqCl:' ', mergeR});
  return <>
    <div css={{"@media print": {paddingBottom: '6.5rem', pageBreakInside: 'avoid'}}}>
      <Text variant="h4" css={{marginTop: '1rem',
      }}>{label}</Text>
      试验条件：天气:{orc?.天气爆温} &nbsp;温度：{orc?.温度爆温} ℃ 最大风速：{orc?.风速爆温} m/s 空气最大相对湿度: {orc?.湿度爆温}%；
      <div css={{display: 'inline-flex'}}>试验路面类型：{orc?.路面爆温}</div>
    </div>
    <Table id='ExplosionProof' fixed={ ["5.8%","10%", "10%", "12%","12%", "10%", "10%", "%", "12%","9%"] }
            css={{borderCollapse: 'collapse', "@media print": {marginTop: '-6.5rem'}}} tight miniw={800}>
      <TableHead>
        <TableRow>
          <CCell rowSpan={2}>测量次数</CCell><CCell colSpan={8}>温度（℃）</CCell><CCell rowSpan={2}>结果判定</CCell>
        </TableRow>
        <TableRow>
          <CCell>制动器</CCell><CCell>离合器</CCell><CCell>液压系统</CCell><CCell>行走电机</CCell><CCell>发动机</CCell><CCell>修正值</CCell><CCell>要求值</CCell><CCell>结果</CCell>
        </TableRow>
      </TableHead>
      <TableBody>
        <RepLink ori rep={rep} tag={'ExplosionProof'}>
          {renderRows}
          <TableRow>
            <Cell colSpan={10} css={{whiteSpace: 'pre-wrap'}}>{tail温度试}</Cell>
          </TableRow>
        </RepLink>
      </TableBody>
    </Table>
  </>;
};
/*其它页面
* */
export const tail接地阻= <Text css={{"@media print": {fontSize: '0.875rem'}}}>
  注：1、所有测量都应在相对湿度低于60%的条件下进行。<br/>
  2、如果电阻低于10<sup>3</sup>Ω，则试验电压应采用直流100V；如果电阻大于10<sup>3</sup>Ω，则试验电压应采用直流500V。
</Text>;
//编辑器用的， 没有分组的：
const items接地=[ [undefined,'车架'], [undefined,'门架'],
      ['货架','货叉架'], [undefined,'货叉'],
      ['护顶','护顶架（司机室）'], [undefined,'轮辋'],
      [undefined,'油缸'], ['他部','其他金属部件']
] as any;
//转换给报告页面用， 每2个项目为1行的分组。
const chunked接地 =expandDimension(items接地);
export const itemA接地阻=['天气接地','温度接地','湿度接地', '地阻v','地阻r'];
items接地.forEach(([tag, title]:any, i: number) => {
    const name='地阻'+(tag?? title);
    itemA接地阻.push(name);
});
/**附表2
 * */
export const Resistance=
    React.forwardRef((
        { children, show ,alone=true,redId,nestMd,label,}:InternalItemProps,  ref
    ) => {
      const [getInpFilter]=useMeasureInpFilter(null,itemA接地阻,);
      const {inp, setInp} = useItemInputControl({ ref });
      return <InspectRecordLayout inp={inp} setInp={setInp} getInpFilter={getInpFilter} show={show} redId={redId}
                                  nestMd={nestMd} alone={alone} label={label!}>
        <Text variant="h5">{label}：</Text>
        试验条件：
        <div css={{display: 'flex', flexWrap: 'wrap', alignItems: 'center', justifyContent: 'center'}}>
          <div css={{display: 'inline-flex', alignItems: 'center', flexWrap: 'wrap'}}>
            天气:
            <BlobInputList value={inp?.天气接地 || ''} rows={1} datalist={天气选}
                           style={{display: 'inline-flex', width: '5rem', maxWidth: '100%', fontSize: '1rem',}}
                           listStyle={css({fontSize: '1rem',})}
                           onListChange={v => setInp({...inp, 天气接地: v || undefined})}/>
          </div>
          <div css={{display: 'inline-flex', alignItems: 'center', flexWrap: 'wrap'}}>
            温度：
            <Input value={inp?.温度接地 || ''} size={2} style={{display: 'inline-flex', width: 'unset'}}
                   onChange={e => setInp({...inp, 温度接地: e.currentTarget.value || undefined})}/>
            ℃；
          </div>
          <div css={{display: 'inline-flex', alignItems: 'center', flexWrap: 'wrap'}}>
            空气最大相对湿度:
            <Input value={inp?.湿度接地 || ''} size={2} style={{display: 'inline-flex', width: 'unset'}}
                   onChange={e => setInp({...inp, 湿度接地: e.currentTarget.value || undefined})}/>
            %；
          </div>
        </div>
        接地电阻测量： 要求值 ≤10<sup>6</sup> Ω；
        <LineColumn column={6}>
          {items接地.map(([tag, title]:any, i: number) => {
            const name='地阻'+(tag?? title);
            return <InputLine key={i} label={title!}>
                      <SuffixInput  value={inp?.[name] ||''}
                            onChange={e=>setInp({ ...inp, [name]: e.currentTarget.value||undefined})}>Ω</SuffixInput>
                </InputLine>
          })}
        </LineColumn>
        试验结果:
        <LineColumn>
          <InputLine  label={'试验结果'}>
            <SuffixInput  value={inp?.地阻v ||''}
                          onChange={e=>setInp({ ...inp, 地阻v: e.currentTarget.value||undefined})}>Ω</SuffixInput>
          </InputLine>
          <InputLine label='C3.11.2结果判定'>
            <SelectInput value={inp?.地阻r || ''} list={检查结论选}
                         onChange={e=>setInp({ ...inp, 地阻r: e.currentTarget.value||undefined})}/>
          </InputLine>
        </LineColumn>
        {tail接地阻}
    </InspectRecordLayout>;
  });
/**附表14 防爆 接地电阻
 * */
const Maxrows=chunked接地.length;
export const ResistanceVw= ({ orc, rep,label,} : { orc: any, rep: any,label:string}
) => {
  return <>
    <div css={{"@media print": {paddingBottom: '7rem', pageBreakInside: 'avoid'}}}>
      <Text variant="h4" css={{marginTop: '1rem',}}>{label}</Text>
      试验条件：天气:{orc?.天气接地} &nbsp;温度：{orc?.温度接地} ℃ 空气最大相对湿度: {orc?.湿度接地}%；
    </div>
    <Table id='Resistance' fixed={ ["18%",  "15%", "16%", "15%",  "11%", "%", "11%"] }
               css={{borderCollapse: 'collapse', "@media print": {marginTop: '-7rem'}}} tight miniw={800}>
      <TableHead>
        <TableRow>
          <CCell colSpan={4}>接地电阻测量</CCell><CCell>要求值</CCell><CCell>试验结果</CCell><CCell rowSpan={2}>结果判定</CCell>
        </TableRow>
        <TableRow><CCell colSpan={6}>单位 Ω</CCell></TableRow>
      </TableHead>
      <TableBody>
        <RepLink ori rep={rep} tag={'Resistance'}>
            {chunked接地.map(([[tag1, title1],[tag2,title2]]:any, i: number) => {
              const name1='地阻'+(tag1?? title1);       //2个一组的，第二个可能是空的！
              const name2=undefined===title2? undefined : '地阻'+(tag2?? title2);
              return <TableRow key={i}>
                <CCell>{title1}</CCell><CCell>{orc?.[name1]}</CCell>
                {name2 ? <><CCell>{title2}</CCell><CCell>{orc?.[name2]}</CCell></>
                    :
                  <><CCell></CCell><CCell></CCell></>
                }
                {0===i && <><CCell rowSpan={Maxrows}>≤10<sup>6</sup></CCell>
                  <CCell rowSpan={Maxrows}>{orc?.地阻v}</CCell><CCell rowSpan={Maxrows}>{orc?.地阻r}</CCell>
                </>}
              </TableRow>
            })}
          <TableRow>
            <Cell colSpan={7} css={{whiteSpace: 'pre-wrap'}}>{tail接地阻}</Cell>
          </TableRow>
        </RepLink>
      </TableBody>
    </Table>
  </>;
};
